<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('业务时间')">
              <j-date
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item :label="$t('会员信息')">
              <a-input :placeholder="$t('请输入手机号/卡号')" v-model="queryParam.memberInfo"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('商品信息')">
                <a-input :placeholder="$t('请输入商品编码/名称')" v-model="queryParam.goodsInfo"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('操作员')">
                <a-select v-model="queryParam.createBy">
                  <a-select-option :key="index" v-for="(item, index) in userList" :value="item.value">{{
                    item.text
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="7" :lg="8" :md="9" :sm="24">
              <a-form-item :label="$t('设备名称(编码)')">
                <a-select show-search :filter-option="false" @search="handleSearch" v-model="queryParam.deviceCode">
                  <a-select-option :value="item.id" :key="index" v-for="(item, index) in deviceList">
                    <span>{{ item.name }}</span>
                    <span v-if="item.name != $t('全部')">({{ item.id }})</span>
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{$t('查询')}}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{$t('重置')}}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
              <a-button
              v-has="'BisGiftReclaimRecordController:export'"
                type="primary"
                icon="download" :loading="exloading"
                style="margin-left: 8px"
                @click="handleExportXls($t('礼品回收记录'))"
                >{{$t('导出')}}</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{$t('无图片')}}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{$t('无文件')}}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            {{$t('下载')}}
          </a-button>
        </template>
        <template slot="goodsName" slot-scope="text, record">
          <a v-if="record.mealList" @click.stop="queryMealDtl(record)">{{text}}</a>
          <span v-else>{{text}}</span>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">{{$t('编辑')}}</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">{{$t('更多')}} <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">{{$t('详情')}}</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm :title="$t('确定删除吗?')" @confirm="() => handleDelete(record.id)">
                  <a>{{$t('删除')}}</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
        <template slot="footer">
          <div style="font-weight: bold">
            <span style="margin-left: 15px;margin-right: 5px">{{$t('回收数量')}}：</span>
            <span>{{ (totalInfo.number||totalInfo.number===0||totalInfo.number==='0')?totalInfo.number:0 }}</span>
            <span style="margin-left: 15px;margin-right: 5px" v-if="operateCoin == 1">{{$t('回收代币')}}：</span>
            <span v-if="operateCoin == 1">{{ (totalInfo.coin||totalInfo.coin===0||totalInfo.coin==='0')?totalInfo.coin.toFixed(2):"0.00" }}</span>
            <span style="margin-left: 15px;margin-right: 5px" v-if="operateLottery == 1">{{$t('回收彩票')}}：</span>
            <span v-if="operateLottery == 1">{{ (totalInfo.lottery||totalInfo.lottery===0||totalInfo.lottery==='0')?totalInfo.lottery.toFixed(0):0 }}</span>
            <span style="margin-left: 15px;margin-right: 5px" v-if="operateCredit == 1">{{$t('回收积分')}}：</span>
            <span v-if="operateCredit == 1">{{ (totalInfo.integral||totalInfo.integral===0||totalInfo.integral==='0')?totalInfo.integral.toFixed(0):0 }}</span>
            <span style="margin-left: 15px;margin-right: 5px" v-if="operateALottery == 1">{{$t('回收A票')}}：</span>
            <span v-if="operateALottery == 1">{{ (totalInfo.alottery||totalInfo.alottery===0||totalInfo.alottery==='0')?totalInfo.alottery.toFixed(0):0 }}</span>
            <span style="margin-left: 15px;margin-right: 5px" v-if="operateACoin == 1">{{$t('回收A币')}}：</span>
            <span v-if="operateACoin == 1">{{ (totalInfo.acoin||totalInfo.acoin===0||totalInfo.acoin==='0')?totalInfo.acoin.toFixed(0):0 }}</span>
          </div>
        </template>
      </a-table>
    </div>
    <meal-detail-modal ref="mealDtlModal" />
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'
import { getAction } from '@/api/manage'
import MealDetailModal from './modules/MealDetailModal'

export default {
  name: 'BisGiftReclaimRecordList',
  mixins: [JeecgListMixin,mixinDevice],
  components: {
    MealDetailModal
  },
  data() {
    return {
      description: '礼品回收记录管理页面',
      moment,
      // 表头
      columns: [
        {
          title:this.$t('业务时间'),
          align:"center",
          dataIndex: 'createTime'
        },
        {
          title:this.$t('手机号'),
          align:"center",
          dataIndex: 'phone'
        },
        {
          title:this.$t('卡号'),
          align:"center",
          dataIndex: 'cardNo'
        },
        {
          title:this.$t('商品编码'),
          align:"center",
          dataIndex: 'goodsCode'
        },
        {
          title:this.$t('商品名称'),
          align:"center",
          dataIndex: 'goodsName',
          scopedSlots: { customRender: 'goodsName' },
        },
        {
          title:this.$t('回收数量'),
          align:"right",
          dataIndex: 'number',
          customRender: (text, r) => {
            return r.number.toFixed(0);
          }
        },
        {
          title:this.$t('设备编码'),
          align:"center",
          dataIndex: 'deviceCode'
        },
        {
          title:this.$t('设备名称'),
          align:"center",
          dataIndex: 'deviceName'
        },
        {
          title:this.$t('操作员'),
          align:"center",
          dataIndex: 'createBy'
        }
      ],
      url: {
        list: "/member/bisGiftReclaimRecord/list",
        delete: "/member/bisGiftReclaimRecord/delete",
        deleteBatch: "/member/bisGiftReclaimRecord/deleteBatch",
        exportXlsUrl: "/member/bisGiftReclaimRecord/exportXls",
        importExcelUrl: "member/bisGiftReclaimRecord/importExcel",
        totalUrl: "/member/bisGiftReclaimRecord/total"
      },
      dictOptions: {},
      deviceList: [],
      userList: [],
      queryParam: {
        createBy: '',
        deviceCode: '',
      },
      isfooter:true,
      totalInfo:{},
      operateCoin: localStorage.getItem('operateCoin'),
      operateLottery: localStorage.getItem('operateLottery'),
      operateCredit: localStorage.getItem('operateCredit'),
      operateALottery: localStorage.getItem('operateALottery'),
      operateACoin: localStorage.getItem('operateACoin')
    }
  },
  created() {
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    queryMealDtl(record){
      this.$refs.mealDtlModal.show(record)
    },
    initDictConfig() {
      if (localStorage.getItem('operateCoin') == 1) {
        this.columns.splice(this.columns.length - 3, 0, {
          title:this.$t('回收代币'),
          align:"right",
          dataIndex: 'coin',
          customRender: (text, r) => {
            return text.toFixed(2);
          }
        })
      }
      if (localStorage.getItem('operateLottery') == 1) {
        this.columns.splice(this.columns.length - 3, 0, {
          title:this.$t('回收彩票'),
          align:"right",
          dataIndex: 'lottery',
          customRender: (text, r) => {
            return text.toFixed(0);
          }
        })
      }
      if (localStorage.getItem('operateCredit') == 1) {
        this.columns.splice(this.columns.length - 3, 0, {
          title:this.$t('回收积分'),
          align:"right",
          dataIndex: 'integral',
          customRender: (text, r) => {
            return text.toFixed(0);
          }
        })
      }
      if (localStorage.getItem('operateALottery') == 1) {
        this.columns.splice(this.columns.length - 3, 0, {
          title:this.$t('回收A票'),
          align:"right",
          dataIndex: 'alottery',
          customRender: (text, r) => {
            return text.toFixed(0);
          }
        })
      }
      if (localStorage.getItem('operateACoin') == 1) {
        this.columns.splice(this.columns.length - 3, 0, {
          title:this.$t('回收A币'),
          align:"right",
          dataIndex: 'acoin',
          customRender: (text, r) => {
            return text.toFixed(0);
          }
        })
      }
      initDictOptions('sys_user,realname,realname').then((res) => {
        this.userList = res.result
        this.userList.unshift({ value: '', text: this.$t('全部') })
      })
      this.handleSearch('')
    },
    handleSearch(value) {
      let query={}
      if(value){
        query.deviceInfo= value
      }
      getAction('/device/bisDeviceInfo/list',query).then((res) => {
        if (res.success) {
          this.deviceList = res.result.records
          this.deviceList.unshift({ id: '', name: this.$t('全部') })
        }
      })
    },
    searchReset() {
      this.handleSearch('')
      this.queryParam = {
        createBy: '',
        deviceCode: '',
      }
      this.selectedRowIndex = []
      this.loadData(1)
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>